<?php
class resource_api_image extends resource_api_abstract {
	
	public static function create($src, $rigorous = true) {
		if(empty($src)) {
			throw new resource_exception("Image is empty.");
		}
		$content = file_get_contents($src['tmp_name']);
		if(!$content) {
			throw new resource_exception("Image is empty.");
		}
		$thumb = system_image_factory::create($src['tmp_name']);
		$ext = strtolower($thumb->getFormat());
		$filter = resource_config_image::getExtensions();
		if(empty($ext) || !in_array($ext, $filter)) {
			throw new resource_exception("Image type is not suppoted,it should be a(n) '" . implode(',', $filter) . "' file.");
		}
		$cDim = $thumb->getCurrentDimensions();
		if($rigorous) {
			$cDim['width'] = intval($cDim['width']);
			$cDim['height'] = intval($cDim['height']);
			$max = resource_config_image::getMaxDimension();
			$max['width'] = intval($max['width']);
			$max['height'] = intval($max['height']);
			if(($max['width'] > 0 && $cDim['width'] > $max['width']) && ($max['height'] > 0 && $cDim['height'] > $max['height'])) {
				throw new resource_exception("This image is too large,please less than {$max['width']} x {$max['height']}, {$cDim['width']} x {$cDim['height']}.");
			}
			$min = resource_config_image::getMinDimension();
			$min['width'] = intval($min['width']);
			$min['height'] = intval($min['height']);
			if(($min['width'] > 0 && $cDim['width'] < $min['width']) && ($min['height'] > 0 && $cDim['height'] < $min['height'])) {
				throw new resource_exception("This image is too small,please more than {$min['width']} x {$min['height']}.");
			}
		}
		$md5 = system_file_io::md5Filename(null, $content);
		$md5code = resource_config_image::md5Path($md5);
		$md5file = resource_config_image::dir() . $md5code;
		$md5name = '/original.' . $ext;
		if(!file_exists($md5file . $md5name)) {
			system_file_io::mkdir($md5file);
			if(!file_put_contents($md5file . $md5name, $content)) {
				throw new resource_exception("Save file faild,Please check if the folder is writable.");
			}
		}
		try {
			$row = self::saveToDatabase(array(
					'hash' => $md5, 
					'htype' => 'image', 
					'size' => $src['size'], 
					'ext' => $ext, 
					'dimension' => $cDim
				));
			if(!parent::$_isInDtabase) {
				if(1 && $watermark = resource_config_image::getWatermark()) {
					$fn = isset($watermark['filename']) ? $watermark['filename'] : null;
					$op = isset($watermark['options']) ? $watermark['options'] : array();
					self::setWatermark($fn, $op);
				}
				if(1 && $watermark = self::getWatermark()) {
					$thumb->setWatermark($watermark);
				}
				$watermark = self::getWatermark();
				foreach(resource_config_image::getSizes() as $key => $name) {
					$image = $thumb->loadImage()->resize($key);
					if($watermark) {
						$image->setWatermark($watermark);
					}
					$image->save($md5file . '/' . $name . '.' . $ext);
				}
			}
			return $row;
		} catch (Exception $ex) {
			if(!parent::$_isInDtabase) {
				@unlink($md5file . $md5name);
				foreach(resource_config_image::getSizes() as $key => $name) {
					@unlink($md5file . '/' . $name . '.' . $ext);
				}
			}
			throw $ex;
		}
	}
	
	protected static $_watermark = null;
	public static function setWatermark($filename = null, $options = array()) {
		if($filename === null) {
			$filename = _DIRECTORY_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'watermark.png';
		}
		self::$_watermark = new system_image_watermark($filename, $options);
	}
	
	/**
	 * @return system_image_watermark
	 */
	public static function getWatermark() {
		return self::$_watermark;
	}

}
